package com.jiinfo.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.base.BaseController;
import com.jiinfo.model.Resource;
import com.jiinfo.model.Role;
import com.jiinfo.model.RoleResource;

public class RoleController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@ActionKey("/manage/role/")
	public void manage_role(){
		
		Record record=new Record();
		record.set("searchContent", getPara("searchContent"));
		Page<Record> page=Role.dao.pagequery(getParaToInt("pageNumber"), getParaToInt("pageSize"), record);
		
		
		setAttr("page", page);
		setAttr("record", record);
		setAttr("pageTitle", getPageTitle("角色管理", "",
				new String[]{"<i class='icon-home'></i>首页","角色管理"},
				new String[]{"/manage","/manage/role/"}));
		renderJsp("/manage/role/role-list.jsp");
		
	}
	
	@ActionKey("/manage/role/add/disp")
	public void manage_role_add_disp(){
		
		String childName="child";
		List<Record> resourceTree=Resource.dao.getTreeResources(childName);
		List<Record> list=new ArrayList<>();
		for (Record record : resourceTree) {
			getListForTree(childName,record,list);
		}
		setAttr("resourceList", list);
		setAttr("pageTitle", getPageTitle("角色管理", "",
				new String[]{"<i class='icon-home'></i>首页","角色管理","角色新增"},
				new String[]{"/manage","/manage/role/","/manage/role/add/disp"}));
		renderJsp("/manage/role/role-add.jsp");
		
	}
	
	@ActionKey("/manage/role/add")
	public void manage_role_add(){
		
		Record user=getCurrUser();
		
		String[] cks=getParaValues("ck");
		Role vo=getModel(Role.class,"vo");
		
		Role.dao.add(vo, cks,user);
		
		redirect("/manage/role/");
		
		
		
	}
	
	@ActionKey("/manage/role/edit/disp")
	public void manage_role_edit_disp(){
		
		String childName="child";
		String id=getPara("id");
		Role role=Role.dao.findById(id);
		List<Record> resourceTree=Resource.dao.getTreeResources(childName);
		List<Record> list=new ArrayList<>();
		for (Record record : resourceTree) {
			getListForTree(childName,record,list);
		}
		
		List<Record> roleResources=RoleResource.dao.getRoleResourceByRoleId(id);
		for (Record record : list) {
			for (Record roleResource : roleResources) {
				if(record.getStr("id").equals(roleResource.getStr("resourceId"))){
					record.set("checked", true);
					break;
				}
			}
		}
		
		setAttr("resourceList", list);
		setAttr("vo", role);
		setAttr("pageTitle", getPageTitle("角色管理", "",
				new String[]{"<i class='icon-home'></i>首页","角色管理","角色编辑"},
				new String[]{"/manage","/manage/role/","/manage/role/edit/disp"}));
		renderJsp("/manage/role/role-edit.jsp");
		
	}
	
	@ActionKey("/manage/role/edit")
	public void manage_role_edit(){
		
		Record user=getCurrUser();
		
		String[] cks=getParaValues("ck");
		Role vo=getModel(Role.class,"vo");
		
		Role.dao.edit(vo, cks,user);
		
		redirect("/manage/role/");
		
	}
	
	@ActionKey("/manage/role/delete")
	public void manage_role_delete(){
		
		String id=getPara("id");
		Role.dao.delete(id);
		
		redirect("/manage/role/");
		
	}

}
